package com.ideasindustry.photoontaje.service.providers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ideasindustry.photoontaje.api.PictureLightVO;

public abstract class PictureProviderBase implements PictureProvider {

	private List<PictureIteratorListener> listeners;
	
	private static final Logger LOG = Logger.getLogger(PictureProviderBase.class);
	
	protected Map<String, String> tags;
	
	public PictureProviderBase(Tag[] tags) {
		
		List<Tag> requiredTags = Arrays.asList(getProviderConnectionTagKeys());
		this.tags = new HashMap<String, String>();
		
 		if(tags!=null){
			 	for (Tag tag: tags){
			 		if (requiredTags.contains(tag)){
			 			this.tags.put(tag.getKey(), tag.getValue());
			 		}
			 	}	
		} else {
			LOG.error("given tags are null"); 			
		}
	}

	public abstract PictureProviderIterator getIterator();
	
	public abstract Tag[] getProviderConnectionTagKeys();
	
	public void iterate() {
		PictureProviderIterator iter = getIterator();
		while (iter.hasNext()) {
			fiereEvent(iter.next());
		}

	}

	public void addListener(int arg0, PictureIteratorListener arg1) {
		getListeners().add(arg0, arg1);
	}

	public boolean addListener(PictureIteratorListener arg0) {
		return getListeners().add(arg0);
	}

	public boolean containsListener(Object arg0) {
		return getListeners().contains(arg0);
	}

	public PictureIteratorListener removeListener(int arg0) {
		return getListeners().remove(arg0);
	}

	public boolean removeListener(Object arg0) {
		return getListeners().remove(arg0);
	}

	public PictureIteratorListener setListener(int arg0,
			PictureIteratorListener arg1) {
		return getListeners().set(arg0, arg1);
	}

	private void fiereEvent(PictureLightVO picture) {
		for (PictureIteratorListener listener : getListeners()) {
			listener.nextPicture(picture);
		}

	}

	public List<PictureIteratorListener> getListeners() {
		if (listeners == null) {
			listeners = new ArrayList<PictureIteratorListener>();
		}
		return listeners;
	}

}
